package net.gyapp.markim.utils;

import android.os.Build;
import android.provider.Settings;
import android.text.TextUtils;

import com.easyder.wrapper.utils.LogUtils;

import net.gyapp.markim.WrapperApplication;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;


/**
 * Auther:  winds
 * Data:    2018/5/7
 * Version: 1.0
 * Desc:
 */


public class DeviceUtils {





    public static String getPseudo() {
        StringBuilder uniqueId = new StringBuilder();
        uniqueId.append(Build.BOARD.length() % 10);
        uniqueId.append(Build.BRAND.length() % 10);
        uniqueId.append(Build.CPU_ABI.length() % 10);
        uniqueId.append(Build.DEVICE.length() % 10);
        uniqueId.append(Build.DISPLAY.length() % 10);
        uniqueId.append(Build.HOST.length() % 10);
        uniqueId.append(Build.ID.length() % 10);
        uniqueId.append(Build.MANUFACTURER.length() % 10);
        uniqueId.append(Build.MODEL.length() % 10);
        uniqueId.append(Build.PRODUCT.length() % 10);
        uniqueId.append(Build.TAGS.length() % 10);
        uniqueId.append(Build.TYPE.length() % 10);
        uniqueId.append(Build.TIME % 10);
        uniqueId.append(Build.USER.length() % 10);

        return uniqueId.toString();
    }

    public static String getDeviceId() {
        //默认取设备号
        String sign = Settings.Secure.getString(WrapperApplication.getInstance().getContentResolver(), Settings.Secure.ANDROID_ID);
        //设备号为空取其他值
        if (TextUtils.isEmpty(sign)) {
            try {
                sign = DeviceUtils.toMD5(DeviceUtils.getPseudo());
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
        }
        return sign;
    }




    public static String toMD5(String text) throws NoSuchAlgorithmException {
        //获取摘要器 MessageDigest
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        //通过摘要器对字符串的二进制字节数组进行hash计算
        byte[] digest = messageDigest.digest(text.getBytes());

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < digest.length; i++) {
            //循环每个字符 将计算结果转化为正整数;
            int digestInt = digest[i] & 0xff;
            //将10进制转化为较短的16进制
            String hexString = Integer.toHexString(digestInt);
            //转化结果如果是个位数会省略0,因此判断并补0
            if (hexString.length() < 2) {
                sb.append(0);
            }
            //将循环结果添加到缓冲区
            sb.append(hexString);
        }
        //返回整个结果
        return sb.toString();
    }
}
